package com.dianning.service.imp;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import org.apache.commons.logging.*;

import com.dianning.bean.AdSpace;
import com.dianning.bean.Area;
import com.dianning.bean.TradingArea;
import com.dianning.bean.Visitor;
import com.dianning.comm.Constant;
import com.dianning.dao.AdAdsDAO;
import com.dianning.dao.AdSpaceDAO;
import com.dianning.dao.AreaDAO;
import com.dianning.dao.TradingAreaDAO;
import com.dianning.dao.VisitorDAO;
import com.dianning.exception.DianningException;
import com.dianning.service.AdService;
import com.dianning.service.AreaService;
import com.dianning.service.Cache;
import com.dianning.service.VisitorService;

public class AreaServiceImp implements AreaService {

	private static final Log logger = LogFactory.getLog(AreaServiceImp.class);
	
	private AreaDAO areaDAO;
	private TradingAreaDAO tradingAreaDAO;
	private Cache areaCache;

	public AreaDAO getAreaDAO() {
		return areaDAO;
	}

	public void setAreaDAO(AreaDAO areaDAO) {
		this.areaDAO = areaDAO;
	}

	public TradingAreaDAO getTradingAreaDAO() {
		return tradingAreaDAO;
	}

	public void setTradingAreaDAO(TradingAreaDAO tradingAreaDAO) {
		this.tradingAreaDAO = tradingAreaDAO;
	}
	
	public Cache getAreaCache() {
		return areaCache;
	}

	public void setAreaCache(Cache areaCache) {
		this.areaCache = areaCache;
	}

	/**
	 * 根据主健获取Area对象
	 * 
	 * @param id
	 * 			long
	 * @return Area
	 * 			Area对象
	 */
	public Area findAreaById(long id){
		if(Constant.USE_CACHE){
			Area area = (Area)this.getAreaCache().get("[area]["+id+"]");
			if(area == null){
				area = this.getAreaDAO().findAreaById(id);
				this.getAreaCache().add("[area]["+id+"]",area);
			}
			return area;
		}else{
			return this.getAreaDAO().findAreaById(id);
		}
	}

	/**
	 * 获取Area对象列表
	 * 
	 * @param orderby
	 * @param ascOrDesc
	 * @return List
	 * 			Area对象列表
	 */
	public List findAreas(String orderby, int ascOrDesc){
		if(Constant.USE_CACHE){
			List l = (List) this.getAreaCache().get("[area][all]["+orderby+"]["+ascOrDesc+"]");
			if(l == null){
				l = this.getAreaDAO().findAreas(orderby, ascOrDesc);
				this.getAreaCache().add("[area][all]["+orderby+"]["+ascOrDesc+"]",l);
			}
			return l;
		}else{
			return this.getAreaDAO().findAreas(orderby, ascOrDesc);
		}
	}
	
	/**
	 * 根据主键ID获取TradingArea对象
	 * @param id
	 * 			long
	 * @return TradingArea
	 * 			TradingArea对象
	 */
	public TradingArea findTradingAreaById(long id){
		if(Constant.USE_CACHE){
			TradingArea tradingArea = (TradingArea) this.getAreaCache().get("[tradingArea]["+id+"]");
			if(tradingArea == null){
				tradingArea = this.getTradingAreaDAO().findTradingAreaById(id);
				this.getAreaCache().add("[tradingArea]["+id+"]",tradingArea);
			}
			return tradingArea;
		}else{
			return this.getTradingAreaDAO().findTradingAreaById(id);
		}
	}

	/**
	 * 根据AreaId获取TradingArea对象列表
	 * @param areaId
	 * 			long
	 * @param orderby
	 * 			String
	 * @param ascOrDesc
	 * 			int
	 * @return List
	 * 			TradingArea对象列表
	 */
	public List findTradingAreasByAreaId(long areaId,String orderby, int ascOrDesc){
		return this.getTradingAreaDAO().findTradingAreasByAreaId(areaId, orderby, ascOrDesc);
	}
}
